//Implement strStr().
//
// Return the index of the first occurrence of needle in haystack, or -1 if need
//le is not part of haystack.
//
// Example 1:
//
//
//Input: haystack = "hello", needle = "ll"
//Output: 2
//
//
// Example 2:
//
//
//Input: haystack = "aaaaa", needle = "bba"
//Output: -1
//
//
// Clarification:
//
// What should we return when needle is an empty string? This is a great questio
//n to ask during an interview.
//
// For the purpose of this problem, we will return 0 when needle is an empty str
//ing. This is consistent to C's strstr() and Java's indexOf().
// Related Topics 双指针 字符串
// 👍 506 👎 0


package leetcode.editor.cn;
public class P28ImplementStrstr{
  public static void main(String[] args) {
       Solution solution = new P28ImplementStrstr().new Solution();
       //TO TEST
  }
  //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int strStr(String haystack, String needle) {
        if(haystack.length() < needle.length()){
            return -1;
        }
        if(needle.length() == 0){
            return 0;
        }
        int l = haystack.length() - needle.length();
        for (int i = 0; i <= l; i++) {
            int c = i;
            for (int j = 0; j < needle.length(); j++) {
                if(haystack.charAt(c) == needle.charAt(j)){
                    if(j == needle.length()-1){
                        return i;
                    }
                    c ++;
                }else {
                    break;
                }
            }
        }
        return -1;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
